<template>
    <base-dialog  v-bind="$attrs" v-on="$listeners" :title="title" width="600px" @submit="toAdd" :no-button="disabled">
        <el-form  ref="elForm" label-width="80px" :model="formData" :rules="rules" :disabled="disabled">
            <el-form-item label="分类" prop="category">
                <select-tree :list="categoryList"   :disable-branch-nodes="true" style="width: 100%" v-model="formData.category" size="small"></select-tree>
            </el-form-item>
            <el-form-item label="物料" prop="name">
                <el-input v-model="formData.name"></el-input>
            </el-form-item>
            <el-form-item label="规格" prop="specs">
                <el-input v-model="formData.specs"></el-input>
            </el-form-item>
            <el-form-item label="单位" prop="unit">
                <el-input v-model="formData.unit"></el-input>
            </el-form-item>
            <el-form-item label="价格" prop="price">
                <el-input-number v-model="formData.price"  :min="0"></el-input-number>
            </el-form-item>
            <el-form-item label="排序" prop="no_order">
                <el-input-number v-model="formData.no_order"></el-input-number>
            </el-form-item>
            <el-form-item label="上架" prop="status">
                <el-radio-group v-model="formData.status">
                    <el-radio
                            v-for="(item, index) in statusOptions"
                            :key="index"
                            :label="item.value"
                            :disabled="item.disabled"
                    >{{ item.label }}</el-radio
                    >
                </el-radio-group>
            </el-form-item>
        </el-form>
    </base-dialog>
</template>
<script>
    import BaseDialog from "../../../components/Common/BaseDialog";
    import DetailDialog from "@/mixins/DetailDialog";
    import category from "@/api/goodsManagement/category/index"
    import SelectTree from "../../../components/Common/SelectTree";
    import XEUtils from "xe-utils";
    export default {
        name:"DetailDialog",
        mixins:[DetailDialog],
        data(){
          return {
              categoryList:[],
              formData:{
                  name:"",
                  category:undefined,
                  price:1,
                  specs:undefined,
                  unit:undefined,
                  status:true,
                  no_order:0
              },
              rules:{
                  name: [
                      { required: true, message: "物品名不能为空", trigger: "blur" }
                  ],
              },
              statusOptions: [
                  {
                      label: "是",
                      value: true
                  },
                  {
                      label: "否",
                      value: false
                  }
              ],
          }
        },
        created() {
            this.getCategoryList()
        },
        components: {SelectTree, BaseDialog},
        computed:{
            title() {
                return this.row.id?"编辑物品":"新增物品"
            },
        },
        methods:{
            getCategoryList(){
                category.getList().then(res=>{
                    this.categoryList = XEUtils.toArrayTree(res.data,{strict: true, parentKey: 'parent', key: 'id', children: 'children'});
                })
            }
        }
    }
</script>
